<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="请输入关键字"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addSport" style="float: right" type="primary">新建运动</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="sportList" stripe v-loading="loading">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="细节：">
                <el-input v-show="props.row.edit" size="small" v-model="props.row.detail"></el-input>
                <span v-show="!props.row.edit">{{props.row.detail}}</span>
              </el-form-item>
              <el-form-item label="方法：">
                <el-input v-show="props.row.edit" size="small" v-model="props.row.method"></el-input>
                <span v-show="!props.row.edit">{{props.row.method}}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号"  width="100px"></el-table-column>
        <el-table-column prop="name" label="名称" width="250px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.name"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="编码" >
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.code"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.code}}</span>
          </template>
        </el-table-column>
        <el-table-column label="运动方式" >
          <template slot-scope="scope">
            <el-button size="mini" @click="setSportMethodBtn(scope.row)">设置</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="small" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="small" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="small" v-if="!scope.row.edit">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                     background
                     layout="prev, pager, next"
                     :page-count="totalPage"
                     @current-change="goPage">
      </el-pagination>
    </el-row>
    <el-dialog v-if="currSport && setSportMethodDialog" :visible.sync="setSportMethodDialog">
      <sport-method :sport="currSport"></sport-method>
    </el-dialog>
  </section>
</template>

<script>
  import sportMethod from './sportMethod'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "sport",
    components: {sportMethod},
    data() {
      return {
        sportList:[],
        totalPage:0,
        loading:false,
        searchKey:null,
        currSport:null,
        setSportMethodDialog:false
      }
    },
    created(){
      //加载运动库列表
      this.getSportList()
    },
    computed:{
    },
    watch: {
      searchKey(nv,ov){
        if(nv === ''){
          this.getSportList()
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      searchBtn() {
        this.getSportList('', this.searchKey)
      },
      goPage(val) {
        this.getSportList(val - 1)
      },
      getSportList(pageNo, key) {
        this.fetch({name: "getSportList", data: {page: pageNo ? pageNo : 0, key: key ? key : ''}})
          .then(res => {
            console.log(res)
            this.sportList = res.content
            this.totalPage = res.totalPages
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      addSport() {
        //新建一个空的addAcupoint，表格页面+1栏
        this.fetch({name: "createSport", data: {name: '', detail: ''}})
          .then(res => {
            console.log(res)
            res.edit = true
            this.sportList.unshift(res)
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      confirm(row) {
        if (row.edit === undefined) {
          Vue.set(row, 'edit', true)//后续添加属性，需要vue联动
          return
        }
        if (row.edit) {
          this.loading = true
          this.updateSport(row)
        } else {
          row.edit = true
        }
      },
      cancelBtn(row) {//取消按钮
        if (row.edit === undefined) {
          Vue.set(row, 'edit', false)//后续添加属性，需要vue联动
        } else {
          row.edit = false
        }
      },
      del(scope) {//删除按钮
        this.loading = true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name: "delSport", data: {id: scope.row.id}})
              .then(res => {
                this.loading = false
                this.sportList.splice(scope.$index, 1)
              })
              .catch(err => {
                console.log(err)
                errHandler.handle(this, err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateSport(row) {
        this.fetch({name: "updateSport", data: {...row}})
          .then(res => {
            this.loading = false
            console.log(res)
            row.edit = false
          })
          .catch(err => {
            console.log(err)
            errHandler.handle(this, err)
          })
      },
      setSportMethodBtn(row){
        this.currSport=row
        this.setSportMethodDialog=true
      }
    }
  }
</script>

<style scoped>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

</style>
